package edu.mit.media.fv.aqview
{
	import edu.mit.media.fv.blocks.*;
	import edu.mit.media.fv.components.*;
	import edu.mit.media.fv.events.*;
	import edu.mit.media.fv.fluidspace.*;
	import edu.mit.media.fv.aqwidgets.*;
	
	import flash.display.*;
	import flash.events.MouseEvent;
	import flash.text.*;
	
	public class AQVoiceBuilder extends AQViewer
	{
		private var isInitialized:Boolean = false;
		
		var _tags:AQTextField;
		var _recorder:AQRecorder;
		var _sendButton:DisplayObject;
		
		public function AQVoiceBuilder()
		{
			super();
			FluidEventHandler.addEventListener(WidgetEvent.DISPLAY_VOICE_BUILDER, displayWidget);
		}
		public function displayWidget(event:WidgetEvent = null):void{
			if(!isInitialized){
				this._recorder.addEventListener(AQRecorderEvent.START_PLAY, pressStartPlay);
				this._recorder.addEventListener(AQRecorderEvent.STOP_PLAY, pressStopPlay);
				this._recorder.addEventListener(AQRecorderEvent.START_RECORD, pressStartRecord);
				this._recorder.addEventListener(AQRecorderEvent.STOP_RECORD, pressStopRecord);
				isInitialized = true;
			}
			FluidEventHandler.dispatchEvent(new WidgetEvent(WidgetEvent.DISPLAY_WIDGET, this, "VMail"));
			if(event.id == -1 || event.id == -15){
				this.id = event.id;
				this._tags.text = "";
			}else{
				this._tags.text = "RE: "+event.subject;
				this.id = event.id;
			}
			
			
		}
		
		public function pressStartRecord(event:AQRecorderEvent):void{
			var data:Object = new Object();
			data.command = FluidConstants.REQUEST_START_RECORD;
			data.id = AudioEvent.TEMP_AUDIO_ID;
			var e:FVNetworkEvent = new FVNetworkEvent(data);
			FluidEventHandler.dispatchEvent(e);
		}
		public function pressStopRecord(event:AQRecorderEvent):void{
			var data:Object = new Object();
			data = new Object();
			data.command = FluidConstants.REQUEST_STOP_RECORD;
			data.id = AudioEvent.TEMP_AUDIO_ID;
			var e:FVNetworkEvent = new FVNetworkEvent(data);
			FluidEventHandler.dispatchEvent(e);
		}
		public function pressStartPlay(event:AQRecorderEvent):void{
			var data:Object = new Object();
			data.command = FluidConstants.REQUEST_START_PLAY;
			data.id = AudioEvent.TEMP_AUDIO_ID;
			var e:FVNetworkEvent = new FVNetworkEvent(data);
			FluidEventHandler.dispatchEvent(e);
		}
		public function pressStopPlay(event:AQRecorderEvent):void{
			var data:Object = new Object();
			data.command = FluidConstants.REQUEST_STOP_PLAY;
			data.id = AudioEvent.TEMP_AUDIO_ID;
			var e:FVNetworkEvent = new FVNetworkEvent(data);
			FluidEventHandler.dispatchEvent(e);
		}
		
		public function sendMessage(event:MouseEvent):void{
			var data:Object = new Object();
			data.command = FluidConstants.REQUEST_SEND_OBJECT;
			data = new Object();
			data.id = this.id;
			data.reply = this.id;
			data.command = FluidConstants.REQUEST_SEND_OBJECT;
			data.type = FluidConstants.VOICE;
			data.tags = this._tags.text;
			data.priority = 1;
			var fvne:FVNetworkEvent = new FVNetworkEvent(data);
			FluidEventHandler.dispatchEvent(fvne);
			
			FluidEventHandler.dispatchEvent(new WidgetEvent(WidgetEvent.DISPLAY_WORKSPACE));
		}
	}
}